package com.lhb.service.Impl;

import com.lhb.dao.CommentDao;
import com.lhb.dao.Repository.CommentReporsitory;
import com.lhb.pojo.BaseResp;
import com.lhb.pojo.Comment;
import com.lhb.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.10.14 下午 22:46
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentReporsitory commentReporsitory;

    @Autowired
    CommentDao commentDao;

    @Override
    public BaseResp findAll(Integer page, Integer limit) {
        PageRequest of = PageRequest.of(page - 1, limit);
        Page<Comment> all = commentReporsitory.findAll(of);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("success");
        baseResp.setData(all.getContent());
        baseResp.setCount(all.getTotalElements());
        return baseResp;
    }

    @Override
    public void del(Integer id) {
        commentReporsitory.deleteById(id);
    }

    @Override
    public BaseResp updateComment(Comment comment) {
        int i = commentDao.updateComment(comment);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("修改成功");
        return baseResp;
    }

    @Override
    public BaseResp AddComment(Comment comment) {
        int i = commentDao.AddComment(comment);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("添加成功");
        return baseResp;
    }
}
